﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Drawing;

public partial class Abonent_Details : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        //if (!Page.IsPostBack && Context.Request.QueryString["surname"] != null)
        if (!Page.IsPostBack)
        {
            //cldrDateStart.SelectedDate = DateTime.Today.AddDays(-1);
            cldrDateStart.SelectedDate = DateTime.Today;
            cldrDateStart.VisibleDate = DateTime.Today;
            //cldrDateStop.SelectedDate = DateTime.Today.AddDays(1);
            //cldrDateStop.VisibleDate = DateTime.Today.AddDays(1);
            if (Context.Request.QueryString["surname"] != null)
            {
                dvAbonent.HeaderText = "Карточка абонента " + Context.Request.QueryString["surname"];
                dsApAtlas.SelectCommand = "select * from ApSpdAtlas " + "where surname = '" +
                  Context.Request.QueryString["surname"] + "'";
                Title = Context.Request.QueryString["surname"] + " - карточка абонента";
                dsApAtlas.DataBind();
                // возьмем код АП из таблы мон400
                Int32 MonCode = 0;
                SqlConnection sqlconn = new SqlConnection(dsApOutbox.ConnectionString);
                string sqlQuery = "select id from mon400_abonents where surname = @surname";
                SqlCommand sqlcomm = new SqlCommand(sqlQuery, sqlconn);
                // set parameters
                sqlcomm.Parameters.Add("@surname", SqlDbType.VarChar);
                sqlcomm.Parameters["@surname"].Value = Context.Request.QueryString["surname"]; ;
                try
                {
                    sqlconn.Open();
                    MonCode = (Int32)sqlcomm.ExecuteScalar();
                    sqlconn.Close();
                    //sqlcomm.Dispose();
                    dsApOutbox.SelectParameters["ApCode"].DefaultValue = MonCode.ToString();
                    dsApInbox.SelectParameters["ApCode"].DefaultValue = MonCode.ToString();
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);
                }
                Label1.Text = "mon400 code: " + MonCode.ToString();
                lbtnCard.Text = "Карточка абонента - " + Context.Request.QueryString["surname"];
                gvInbox.Sort("SendTime", SortDirection.Ascending);
                gvOutbox.Sort("SendTime", SortDirection.Ascending);
            };
            //if (!Page.IsPostBack && Context.Request.QueryString["kod"] != null)
            if (Context.Request.QueryString["kod"] != null)
            {
                dsApAtlas.SelectCommand = "select * from ApSpdAtlas " + "where kod = '" +
                  Context.Request.QueryString["kod"] + "'";
                //dsApInbox.SelectParameters["ApCode"].DefaultValue = Context.Request.QueryString["kod"];
                //dsApInbox.SelectParameters["ApCode"].DefaultValue = "365";
                //dsApAtlas.DataBind();
            };
            cldrDateStart_SelectionChanged(sender, e);
        };
        Title = Context.Request.QueryString["surname"];
        //dsApAtlas.DataBind();
    }
    protected void dsApAtlas_Selected(object sender, SqlDataSourceStatusEventArgs e)
    {
        
    }
    protected void dsApAtlas_Deleted(object sender, SqlDataSourceStatusEventArgs e)
    {
        Response.Redirect("Abonents.aspx");
    }
    protected void dsApAtlas_Inserted(object sender, SqlDataSourceStatusEventArgs e)
    {
        Response.Redirect("abonent-details.aspx?surname=" + 
            e.Command.Parameters[e.Command.Parameters.IndexOf("surname")].Value);
      //  dsApAtlas.SelectCommand = "select * from ApSpdAtlas " + "where surname = '" +
      //  Context.Request.QueryString["surname"] + "'";
    }
    protected void btnBack_Click(object sender, EventArgs e)
    {
        Response.Redirect("Abonents.aspx");
    }
    protected void cldrDateStart_SelectionChanged(object sender, EventArgs e)
    {
        dsApOutbox.SelectParameters["StatDate"].DefaultValue = cldrDateStart.SelectedDate.ToShortDateString();
        dsApInbox.SelectParameters["StatDate"].DefaultValue = cldrDateStart.SelectedDate.ToShortDateString();
        //gvInbox.Sort("SendTime,msg_id", SortDirection.Ascending);
        switch(MultiView1.ActiveViewIndex)
        {
            case 1: 
                gvInbox.DataBind();
                break;
            case 2:
                gvOutbox.DataBind();
                break;
        }
    }
    protected void dsApInbox_Selected(object sender, SqlDataSourceStatusEventArgs e)
    {
        lbTotal.Text = "Абонент: " + Context.Request.QueryString["surname"] + ". Дата: " + cldrDateStart.SelectedDate.ToShortDateString() +
            " Всего: " + e.AffectedRows.ToString();
        //lbtnInbox.Text = "Входящие сообщения (" + e.AffectedRows.ToString() + ")";
    }
    protected void lbtnInbox_Click(object sender, EventArgs e)
    {
        MultiView1.SetActiveView(ViewAbonInbox);
        lbTotal.Visible = true;
        cldrDateStart.Visible = true;
        cldrDateStart_SelectionChanged(sender, e);
        Title = Context.Request.QueryString["surname"] + " - Входящие";
        lbtnCard.ForeColor = Color.Empty;
        lbtnInbox.ForeColor = Color.Yellow;
        lbtnOutbox.ForeColor = Color.Empty;
        //gvInbox.Sort("SendTime,msg_id", SortDirection.Ascending);
    }
    protected void lbtnCard_Click(object sender, EventArgs e)
    {
        MultiView1.SetActiveView(ViewAbonCard);
        cldrDateStart.Visible = false;
        lbTotal.Visible = false;
        Title = Context.Request.QueryString["surname"] + " - Карточка абонента";
        lbtnCard.ForeColor = Color.Yellow;
        lbtnInbox.ForeColor = Color.Empty;
        lbtnOutbox.ForeColor = Color.Empty;
    }
    protected void lbtnOutbox_Click(object sender, EventArgs e)
    {
        MultiView1.SetActiveView(ViewAbonOutbox);
        lbTotal.Visible = true;
        cldrDateStart.Visible = true;
        cldrDateStart_SelectionChanged(sender, e);
        Title = Context.Request.QueryString["surname"] + " - Исходящие";
        lbtnCard.ForeColor = Color.Empty;
        lbtnInbox.ForeColor = Color.Empty;
        lbtnOutbox.ForeColor = Color.Yellow;
    }
    protected void gvOutbox_Sorting(object sender, GridViewSortEventArgs e)
    {

    }
    protected void dsApOutbox_Selected(object sender, SqlDataSourceStatusEventArgs e)
    {
        lbTotal.Text = "Абонент: " + Context.Request.QueryString["surname"] + ". Дата: " + cldrDateStart.SelectedDate.ToShortDateString() +
            " Всего: " + e.AffectedRows.ToString();
        //lbtnOutbox.Text = "Исходящие сообщения (" + e.AffectedRows.ToString() + ")";
    }
}
